<template>
  <a-dropdown class="lang header-item" :placement="position">
    <div>
      <a-icon type="global"/> {{langAlias}}
    </div>
    <a-menu @click="val => setLanguage(val.key)" :selected-keys="[lang]" slot="overlay">
      <a-menu-item v-for=" lang in langList" :key="lang.key">{{lang.key.toUpperCase() + ' ' + lang.name}}</a-menu-item>
    </a-menu>
  </a-dropdown>
</template>

<script>
 import {mapState, mapMutations} from 'vuex'

  export default {
    name: 'SelectLang',
    data () {
      return {
        langList: [
          {key: 'CN', name: '简体中文', alias: '简体中文'},
          {key: 'US', name: 'English', alias: 'English'}
        ]
      }
    },
    props: {
      position: {
        type: String,
        default: 'bottomCenter'
      }
    },
    computed: {
      ...mapState('lang', ['lang']),
      langAlias() {
        let lang = this.langList.find(item => item.key == this.lang)
        return lang.alias
      }
    },
    methods: {
      ...mapMutations('lang', ['setLang']),
      setLanguage(key) {
        this.setLang(key)
        const langConfig = {
          lang: key
        }
        localStorage.setItem(process.env.VUE_APP_LANG_KEY, JSON.stringify(langConfig))
      },
    }
  }
</script>

<style scoped lang="less">
</style>
